<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      html,
      body {
        height: 100%;
      }
      #ball {
        background-color: blueviolet;
        width: 50px;
        height: 50px;
        border-radius: 50%;
      }
      * {
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="ball"></div>
    <script>
      window.addEventListener('click', function (e) {
        const dom = document.getElementById('ball');
        const x = e.clientX - dom.offsetWidth / 2;
        const y = e.clientY - dom.offsetHeight / 2;
        move(x, y);
      });

      // 设置动画
      function move(x, y) {
        const dom = document.getElementById('ball');

        const rect = dom.getBoundingClientRect();
        const r_x = rect.x;
        const r_y = rect.y;
        const animate = dom.animate(
          [
            {
              transform: `translate(${r_x}px, ${r_y}px)`,
            },
            {
              transform: `translate(${r_x}px, ${r_y}px) scaleX(1.5)`,
            },
            {
              transform: `translate(${x}px,${y}px)`,
            },
          ],
          {
            duration: 500,
            fill: 'forwards',
            // 可是联合这 https://developer.mozilla.org/zh-CN/docs/Web/CSS/animation 一起看
          }
        );
        // 暂停动画
        animate.pause();
        // 启动动画
        animate.play();
      }
    </script>
  </body>
</html>
